﻿using System;

namespace NuGet.Enterprise.Server
{
    /// <summary>
    /// Represents a smart disposable wrapper.
    /// </summary>
    internal sealed class Disposable
        : IDisposable
    {
        #region Statics

        /// <summary>
        /// Get the read only non disposal instance.
        /// </summary>
        public static readonly Disposable NonDispose = new Disposable();

        #endregion

        #region Fields

        private Action _action;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Disposable"/> class.
        /// </summary>
        private Disposable()
        {
            _action = null;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Disposable"/> class.
        /// </summary>
        /// <param name="action">The <see cref="Action"/> to call on dispose.</param>
        public Disposable(Action action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            _action = action;
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (_action == null) return;

            try
            {
                _action();
            }
            finally
            {
                _action = null;
            }
        }

        #endregion
    }

    /// <summary>
    /// Represents a smart disposable wrapper.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    internal sealed class Disposable<T>
        : IDisposable
    {
        #region Fields

        private Action<T> _action;

        private T _value = default(T);

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Disposable"/> class.
        /// </summary>
        /// <param name="action">The <see cref="Action"/> to call on dispose.</param>
        /// <param name="value">The value.</param>
        public Disposable(Action<T> action, T value)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            _action = action;
            _value = value;
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (_action == null) return;

            try
            {
                _action(_value);
            }
            finally
            {
                _action = null;
            }
        }

        #endregion
    }
}
